//
//  STStatusOriginalView.swift
//  STVVeibo
//
//  Created by leergou on 16/7/24.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

import UIKit
import SnapKit

class STStatusOriginalView: UIView {
    
    var originViewBottomConstraint: Constraint?
    // 定义一个 statusViewModel 属性,给子控件赋值
    var statusViewModel: STStatusViewModel? {
        
        didSet{
            // 设置头像
            headImageView.st_setImage(statusViewModel?.status?.user?.profile_image_url, placeholderImgName: "avatar_default")
            // 昵称
            nameLabel.text = statusViewModel?.status?.user?.name
            // 微博创建时间
            timeLabel.text = statusViewModel?.creatTime
            // 微博来源
//            sourceLabel.attributedText = statusViewModel?.status?.sourceAttr
            sourceLabel.attributedText = statusViewModel?.sourceAttr
            // 微博等级
            memberImageView.image = statusViewModel?.memberImage
            // 微博认证
            avatarImageView.image = statusViewModel?.avatarImage
            // 微博内容
            contentLabel.attributedText = statusViewModel?.originalContentAttr
            
            // 正则匹配结果
//            dealContentText(statusViewModel?.status?.text)
            
            // 配图
            /*
            1.卸载约束
            2. 图片数量 > 0
            - 赋值
            - 更新约束
            - 显示图片(cell 重用)
            图片数量 < 0
            - 更新约束
            - 隐藏配图(cell 重用)
            */
            // 第一次用成了 install 所以,导致了约束冲突
            originViewBottomConstraint?.uninstall()
            // 有图片
            if statusViewModel?.status?.pic_urls?.count > 0{
                // 赋值
                pictureView.pictureInfo = statusViewModel?.status?.pic_urls
                // 更新约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                     self.originViewBottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
                })
                // 显示配图
                pictureView.hidden = false
            } else {
                // 更新约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.originViewBottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(statusCellMargin).constraint
                })
                // 隐藏配图
                pictureView.hidden = true
            }
        }
    }
    
  
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 设置视图
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
        // 添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(memberImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)

        // 添加约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(statusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(statusCellMargin)
        }
        
        memberImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(statusCellMargin)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(statusCellMargin)
        }
        
        avatarImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(statusCellMargin)
            make.leading.equalTo(headImageView)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(statusCellMargin)
            make.leading.equalTo((SCREENW - 250) * 0.5)
//            make.size.equalTo(CGSize(width: 80, height: 80)) // 测试数据
        }
        
        // 给当前原创微博设置约束 -> 因为添加了配图,所以要记录此约束
        self.snp_makeConstraints { (make) -> Void in
            self.originViewBottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(statusCellMargin).constraint
        }
    }
    
    //MAKR: lazy
    // 头像
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default")
    // 昵称
    private lazy var nameLabel: UILabel = UILabel(font: 15, textColor: UIColor.blackColor())
    // 微博等级
    private lazy var memberImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    // 微博时间
    private lazy var timeLabel: UILabel = UILabel(font: 10, textColor: ThemeColor)
    // 微博来源
    private lazy var sourceLabel: UILabel = UILabel(font: 10, textColor: ThemeColor)
    // 认证logo
    private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    // 微博内容
    private lazy var contentLabel: UILabel = UILabel(font: 15, textColor: UIColor.darkGrayColor(), maxWidth: SCREENW - 20)
    // 配图
    private lazy var pictureView: STStatusPictureView = STStatusPictureView()
}





